package com.liukai.novel_rack.ui;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.liukai.novel_base.base.BaseActivity;
import com.liukai.novel_commons.Result;
import com.liukai.novel_commons.entities.NovelInfo;
import com.liukai.novel_commons.utils.JSONUtil;
import com.liukai.novel_info.call_back.INovelCallBack;
import com.liukai.novel_info.presenter.impl.NovelPresenterImpl;
import com.liukai.novel_info.ui.NovelDescActivity;
import com.liukai.novel_rack.R;
import com.liukai.novel_rack.R2;
import com.liukai.novel_rack.adapter.HistoryAdapter;
import com.liukai.novel_rack.adapter.HotAdapter;
import com.liukai.novel_rack.search_interface.OnSearchItemListener;

import butterknife.BindView;

public class SearchActivity extends BaseActivity implements INovelCallBack {

    @BindView(R2.id.btn_back)
    TextView btnBack;

    @BindView(R2.id.et_search_content)
    EditText searchContent;

    @BindView(R2.id.btn_search)
    TextView btnSearch;

    @BindView(R2.id.hot_recycler_view)
    RecyclerView hotRecyclerView;
    private LinearLayoutManager mLinearLayoutManager;
    private HotAdapter mHotAdapter;

    @BindView(R2.id.history_recycler_view)
    RecyclerView historyRecyclerView;
    private HistoryAdapter mHistoryAdapter;
    private NovelPresenterImpl mNovelPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    protected void initData(Context context) {

    }

    @Override
    protected void initView() {

        mHotAdapter = new HotAdapter(this);
        mLinearLayoutManager = new LinearLayoutManager(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        hotRecyclerView.setLayoutManager(gridLayoutManager);
        hotRecyclerView.setAdapter(mHotAdapter);

        mHistoryAdapter = new HistoryAdapter();
        historyRecyclerView.setLayoutManager(mLinearLayoutManager);
        historyRecyclerView.setAdapter(mHistoryAdapter);

        mNovelPresenter = new NovelPresenterImpl();
        mNovelPresenter.registerCallback(this);

    }

    @Override
    protected void initListener() {

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String contentText = searchContent.getText().toString();
                if (contentText.equals("")) {
                    Toast.makeText(SearchActivity.this, "请输入内容", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
                intent.putExtra("searchContent", contentText);
                startActivity(intent);
            }
        });

        mHistoryAdapter.setOnSearchItemListener(new OnSearchItemListener() {
            @Override
            public void onItemClick(NovelInfo novelInfo) {
                mNovelPresenter.findNovelById(novelInfo.getNovelId());
//                Toast.makeText(SearchActivity.this, "novelInfo" + novelInfo.toString(), Toast.LENGTH_SHORT).show();
            }
        });

        mHotAdapter.setOnSearchItemListener(new OnSearchItemListener() {
            @Override
            public void onItemClick(NovelInfo novelInfo) {
                mNovelPresenter.findNovelById(novelInfo.getNovelId());
//                Toast.makeText(SearchActivity.this, "novelInfo" + novelInfo.toString(), Toast.LENGTH_SHORT).show();
            }
        });

    }


    @Override
    public void findNovelByIdResult(Result result) {
        if (result.getCode() != 200) {
            Toast.makeText(this, "加载失败", Toast.LENGTH_SHORT).show();
            return;
        }
        NovelInfo novelInfo = JSONUtil.resolverResult(result, NovelInfo.class);
        Intent intent = new Intent(SearchActivity.this, NovelDescActivity.class);
        intent.putExtra("novelInfo", novelInfo);
        startActivity(intent);
    }

    @Override
    public void findNovelChapterByIdResult(Result result) {

    }
}
